<?php
if($_REQUEST['withdraw']=='1')
{	
	header("location:index.php?token_id=2");
}



$sql_comm="SELECT count( * ) TotTrans, sum(refer_comm) AS TotAmt,refer_to_user_id,u.first_name, u.last_name
FROM tbl_refer_comm r
LEFT JOIN tbl_users u ON r.refer_to_user_id = u.user_id
WHERE refer_by_user_id =".$_SESSION['user_id_sess']."
AND isPaid = '0'
GROUP BY refer_to_user_id";

$rst_comm=mysql_query($sql_comm);
//$row_comm=mysql_fetch_array($rst_comm);
$row_comm=getRecordArray($rst_comm);
//print_r($rows);

$sql_comm_user="SELECT  * 
FROM tbl_users u
LEFT JOIN tbl_referer r ON r.refer_to_user_id = u.user_id
WHERE refer_by_user_id =".$_SESSION['user_id_sess']."
AND isActive = '1'";

$rst_comm_user=mysql_query($sql_comm_user);
$counter2=mysql_num_rows($rst_comm_user);
//$row_comm=mysql_fetch_array($rst_comm);
$row_comm_user=getRecordArray($rst_comm_user);
$user_array=array();



function getRow($set_user_id)
{

	$row=array();
	foreach($row_comm as $temp)
	{
		if($temp->refer_to_user_id==$set_user_id)
		{
			$row=$temp;
		}
	}	
	return $row;
}
if($_REQUEST['ref']=='1')
{	
	$strMessage .="Your withdrawal request has been sent successfully<br/>";
}


?>
<div id='panel'>
 <div class="fulldiv" style="width:700px;">

 <div class="fulldiv" style="margin:10px 0px 0px 0px;">
 	<div style="float:left; margin:0px 10px 0px 10px; padding-top:8px;"><strong class="orangehead">Referred Commission</strong></div>
	<div class="greyarrowsml" onclick="show_hide_s_boxes('myswapen_refcomm')" id="arrow_myswapen_refcomm">&nbsp;</div>
	
	<div class="greytab_out" id="other1_myswapen_refcomm">
		<div class="greytab_left">&nbsp;</div>
		<div class="greytab_repeat" onclick="$('#normal_box').hide();$('#refund_only_box').fadeIn('slow');" style="cursor:pointer;">Refund Reffered Commission</div>
		<div class="greytab_right">&nbsp;</div>
	</div>
 </div>

<div id="myswapen_refcomm">
<div class="fullbox_top">&nbsp;</div>
<div class="fullbox_repeat" id="normal_box">
<?php
$counter=count($row_comm);
//echo "counter1:".$counter."<br>";
//echo "counter2:".$counter2."<br>";
if($counter2!=0)
{
?>
<form name="form1" id="form1" method="post" onsubmit="return chk_form();">
<div style="width:370px; margin:0px 15px 0px 0px; padding:0px; float:left;">
  <div style="float: left; width: 370px; border-bottom:2px solid #ccc; text-align:center;">
   <div class="cell_head" style="width: 115px;">Referred User</div>
   <div class="cell_head" style="width: 115px;">Total Transactions</div>
   <div class="cell_head" style="width: 115px;">Total Commission</div>
  </div>

		<?php
		$total=0;
		//print_r($row_comm_user);
		for($i=0;$i<$counter;$i++)
		{
		 $user_array[]=$row_comm[$i]['refer_to_user_id'];
		} 
		//echo "rowcom".$counter_user=count($row_comm_user);
		//echo "vikas". $row_comm_user['0']['user_id'];
		//print_r($user_array);
		$counter_user=count($row_comm_user);
		for($j=0;$j<$counter_user;$j++)
		{
		  // echo "vikas". $row_comm_user[$j]['user_id'];
		  if (in_array($row_comm_user[$j]['user_id'], $user_array))
		  {
		    $sql_comm_user="SELECT count( * ) TotTrans, sum(refer_comm) AS TotAmt,refer_to_user_id,u.first_name, u.last_name
			    FROM tbl_refer_comm r
			    LEFT JOIN tbl_users u ON r.refer_to_user_id = u.user_id
			    WHERE refer_by_user_id =".$_SESSION['user_id_sess']."
			    AND isPaid = '0' AND r.refer_to_user_id=".$row_comm_user[$j]['user_id']." 
			    GROUP BY refer_to_user_id";
		    $rst_comm_user=mysql_query($sql_comm_user);
		    $row_User=mysql_fetch_array($rst_comm_user);
				  
		    $sql_comm_user1="SELECT count( * ) TotTrans, sum(refer_comm) AS TotAmt,refer_to_user_id,u.first_name, u.last_name
			    FROM tbl_refer_comm r
			    LEFT JOIN tbl_users u ON r.refer_to_user_id = u.user_id
			    WHERE refer_by_user_id =".$_SESSION['user_id_sess']."
			    AND isPaid = '0' AND r.refer_to_user_id=".$row_comm_user[$j]['user_id']." AND DATEDIFF( CURDATE( ) , buy_date ) <30
			    GROUP BY refer_to_user_id";
		    $rst_comm_user1=mysql_query($sql_comm_user1);
		    $row_User1=mysql_fetch_array($rst_comm_user1);
		    
		    $rem_amt=$row_User['TotAmt'] - $row_User1['TotAmt'];
		    
		    $total=$total+$rem_amt;
		    
		    $sql="select * from tbl_refer_comm r,tbl_users u where refer_by_user_id =".$_SESSION['user_id_sess']." AND isPaid = '0' AND r.refer_to_user_id=".$row_comm_user[$j]['user_id']." AND r.refer_to_user_id = u.user_id";
		    $rs=mysql_query($sql) or mysql_error();
		    ?>
		    <div style="float:left; width:369px; margin:3px 0px 3px; text-align:center;">
		      <div class="cell_row" style="width: 115px;"><?php echo $row_User['first_name'].' '.$row_User['last_name'];?></div>
		      <div class="cell_row" style="width: 115px;"><a href="javascript:void(0);" onclick="show_popupbox('<?=$row_comm_user[$j]['user_id']?>');" id="displaylist<?=$row_comm_user[$j]['user_id']?>"><?php echo $row_User['TotTrans'];?></a></div>
		      <div class="cell_row" style="width: 115px;" align="left">$<?php echo $rem_amt;?></div>
		    </div>
		    
		    <div id="p<?=$row_comm_user[$j]['user_id']?>" style="width: 400px; background:#fff; border: 3px solid #000000; margin: 0px; padding: 0px; display:none; position: absolute; ">
			<div style="float: left; width: 400px; font-weight: bold;">
			    <span style="padding: 5px; float: right; cursor:pointer;" onclick="$('#p<?=$row_comm_user[$j]['user_id']?>').hide();">x</span>
			    
			</div>
			<div style="float: left; width: 400px; font-weight: bold; background: none repeat scroll 0% 0% #000000; color: #ffffff;">
			    <div style="float: left; width: 100px; margin: 0px 5px;">#</div>
			    <div style="float: left; width: 100px; margin: 0px 5px;">Amount</div>
			    <div style="float: left; width: 160px; margin: 0px 5px;">Time Left</div>
			</div>
			<?
			$i=1;
			while($row=mysql_fetch_array($rs))
			{
				$remainder =0;
				
				//$s=mysql_query("Update tbl_refer_comm set buy_date='2010-12-26' where ref_comm_id='".$row['ref_comm_id']."'");
				
				 $y=date('Y',strtotime($row['buy_date']));
				 $d=date('d',strtotime($row['buy_date']));
				 $m=date('m',strtotime($row['buy_date']));
				
	
				$futureTime = mktime(0, 0, 0, date($m), date($d)+30, date($y));
				$d=date('Y-m-d h:i:s',$futureTime);
				$remainder = getRemainingTime($d);
				
				$r_css = 'style="float: left; width: 400px; background: none repeat scroll 0% 0% #ccccff;"';
				
				if($i%2==0) $r_css = 'style="float: left; width: 400px; background: none repeat scroll 0% 0% #cccccc;"';
				
				
				?>
				<div <?php echo $r_css;?>>
				    <div style="float: left; width: 100px;" align="left"><?=$i?></div>
				    <div style="float: left; width: 100px;" align="left">$<?=$row['refer_comm']?></div>
				    <div style="float: left; width: 160px;" align="left"><?=$remainder?></div>
				</div>
				
				<?
				   $i++;
			}
			?>  
			
			
		    </div>
		    
		    <?php
		  }
		  else
		  { ?>
		    <div style="float:left; width:369px; margin:3px 0px 3px;">
		      <div class="cell_row" style="width: 115px; text-align:center;"><?php echo $row_comm_user[$j]['first_name'].' '.$row_comm_user[$j]['last_name'];?></div>
		      <div class="cell_row" style="width: 115px;">0</div>
		      <div class="cell_row" style="width: 115px;">$0.00</div>
		    </div>
		    
	  <?php	  }
		}
	      ?>
		    <div style="float:left; width:369px; margin:3px 0px 3px;">
		      <div class="cell_row" style="width: 115px;">&nbsp;</div>
		      <div class="cell_row" style="width: 115px;">&nbsp;</div>
		      <div class="cell_head" style="width: 115px;"><font color="red">$<?php echo number_format($total,2);?></font> Total<br/>
		       <input type="hidden"  name="withdraw" id="withdraw" value="<?php echo $withdraw;?>" />
		       <span style="width:150px; margin:3px 0px 0xp 7px; font-size:10px; color:#666666; font-weight:normal;">minus amazon transaction fee</span>
		      </div>
		    </div>
		  
	      
	      

</div>
<div style="width:300px; margin:0px; padding:0px; float:left;">

<table width="100%" border="0">
	<tr>
		<td align="center">
		 <?
		 $sql = mysql_query("select amazon_email from tbl_users where user_id=".$_SESSION['user_id_sess']) or die(mysql_error());
		 $ae = mysql_fetch_array($sql);
		 $amazon_email = $ae['amazon_email'];
		 ?>
		 <input type="hidden" name="amazon_email" id="amazon_email" value="<?php echo $amazon_email;?>" />
		<input class="sbmt_btn" id="imgWithDraw" value="Request Withdrawal" style="height:35px;" type="submit" onclick=""  /><br />
		Need a minimum of $10.00 to request a withdrawal.
		</td>
	</tr>
	
	

</table>
</div>


</form>

<?php

}
else
{
 ?>
  <div style="width:99%; float:left;" align="center">
     <div class="box" style="float:none;">
        <div class="box_top">&nbsp;</div>
        <div class="box_middle">
            <div class="boxtxt_out">
	     <span style="float: left; width: 50%; text-align:left;"><strong>The more people <br/>you refer</strong></span>
	     <span style="float: left; width: 50%; text-align: right;"><strong style="text-align: right;">The more referred <br/>members you have</strong></span>
	     <span style="float: left; width: 100%; margin-top: 15px;"><strong>The more commission you earn</strong></span>
	    </div>
            
		<div class="boxtxt_out">
			<div class="box_img">
			 <img src="images/starthere_yellow.png" style="cursor:pointer;" onclick="window.location='index.php?refer';" />
			</div>
		</div>
        </div>
        <div class="box_bottom">&nbsp;</div>
     </div>
  </div>
 <?
}
?>

</div>

<div class="fullbox_repeat" id="refund_only_box" style="display:none;">
 <div style="float:left; width:90%">
  <a href="javascript:void(0)" onclick="$('#refund_only_box').hide();$('#normal_box').fadeIn('slow');">Back to Referred Commission</a>
 </div>
 <div style="width:390px; margin:0px 15px 0px 0px; padding:0px; float:left;">
  <div style="float: left; width: 370px; margin:10px 0px;">
   <span style="float:left; background:#ED9C9C; width:15px;">&nbsp;</span> &nbsp;&nbsp;&nbsp;Refund Required
  </div>
  <div style="float: left; width: 370px; border-bottom:2px solid #ccc; text-align:center;">
   <div class="cell_head" style="width: 115px;">Referred User</div>
   <div class="cell_head" style="width: 115px;">Commission</div>
   <div class="cell_head" style="width: 115px;">Action</div>
  </div>
  <form name="ref_form" id="ref_form" method="post">
  <?
  $sql="select * from tbl_refer_comm r,tbl_users u where refer_by_user_id =".$_SESSION['user_id_sess']." AND isPaid = '1' AND r.refer_to_user_id = u.user_id";
  $rs=mysql_query($sql) or mysql_error();
  $c_rs = mysql_num_rows($rs);
  if($c_rs > 0)
  {
   $j=1;
   while($row=mysql_fetch_array($rs))
   {
    
    $cccs = 'style="float:left; width:369px; margin:3px 0px 3px; background:#D0F4D0; text-align:center"';
    if($j%2==0) $cccs = 'style="float:left; width:369px; margin:3px 0px 3px; background:#ACE0AC; text-align:center"';
    
    $sql = mysql_query("select * from tbl_orders where transaction_ID='".$row['transaction_id']."' and refund=1") or die(mysql_error());
    $cnt_sql = mysql_num_rows($sql);
    
    if($cnt_sql > 0)
    {
     
     $cccs = 'style="float:left; width:369px; margin:3px 0px 3px; background:#ED9C9C;"';
     if($row['isRefunded']==0)
      $radio = '<input type="radio" name="ref" id="ref'.$row['ref_comm_id'].'" value="'.$row['ref_comm_id'].'" />';
     else
      $radio = 'Refunded';
     
    }
    else
    {
     $radio = '&nbsp;';
    }
    
    
    
    $refer_to_name = getUser_first_last_name($row['refer_to_user_id']);
    ?>
    <div <?php echo $cccs;?>>
     <div class="cell_row" style="width: 115px; text-align:center"><?php echo $refer_to_name['first_name'].' '.$refer_to_name['last_name'];?></div>
     <div class="cell_row" style="width: 115px; text-align:center">$<?php echo $row['refer_comm'];?></div>
     <div class="cell_row" style="width: 115px; text-align:center"><?php echo $radio;?></div>
    </div> 
    <?php
    $j++;
   }
  }
  else {
   echo '<div style="float:left; width:369px; margin:3px 0px 3px;">No Records Available</div>';
  }
  ?>
  </form>
  
 </div>
 
 <div style="width:275px; margin:0px; padding:0px; float:left;">	
  <table width="100%" border="0">
   <tr>
    <td align="center">
     <input class="sbmt_btn" id="imgWithDraw" value="Refund" style="height:35px;" type="button" onclick="return validatechk1()"  />
    </td>
   </tr>
  </table>
 </div>
</div>

<div class="fullbox_bot">&nbsp;</div>
</div>
<hr style="float:left; width:500px; display:none;" id="hr_line_myswapen_refcomm" />

</div>

</div>
<div class="popup_box" style="display:none; position:absolute; background:#FFFFFF;" id="no_amazon"> 
  <div class="popup_box_in" style="height:120px;">
      <div style="float: left; margin: 0 30px; padding: 5px; text-align: left; width: 85%;">
	   To recieve your referred commission funds you need to establish either and Amazon Personal Account(<a href="https://www.amazon.com/ap/signin?_encoding=UTF8&openid.assoc_handle=usflex&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%3Fie%3DUTF8%26ref_%3Dpd_irl_gw_r&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.pape.max_auth_age=0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select">here</a>) or a Business Amazon Account(<a href="https://www.amazon.com/gp/corporate/register/account-info">here</a>).
	   Once esatblished all your referred commission will go to your Amazon account. If you have question visit our <a href="index.php?faq">FAQ</a> or contact us at support@swapen.com
      </div>
      <div style="float:left; width:98%; padding:5px;">
	  <input type="button" name="" value="Close" class="refer_btn" style="margin:0px; height:30px; cursor:pointer;" onclick="javascript:$('#no_amazon').hide();" />
      </div>
  </div>
</div>
<script language="javascript" type="text/javascript">
var varTot='<?php echo $total;?>';
if(varTot=="" || parseInt(varTot)<10)
{
	//document.getElementById("imgWithDraw").style.display='none';
	//document.getElementById("imgWithDraw").style.visiblity='hidden';
}
</script>
<script>
 
 function chk_form()
 {
  var amazon_email = $('#amazon_email').val();
  if(amazon_email=='')
  {
    
    var ntop = (( $(window).height() - $('#imgWithDraw').height()) / 2+$(window).scrollTop()) - 100;
    var nleft = ( $(window).width() - $('#imgWithDraw').width()) / 2+$(window).scrollLeft();

    $('#no_amazon').css({'left': '', 'top':ntop});
    $('#no_amazon').fadeIn('slow');
    
    
    return false;
  }
  else
  {
    $('#withdraw').val('1');
    return true;
  }
 }
 
 function show_popupbox(id)
 {
  var pos = $('#displaylist'+id).position();
  var nleft = pos.left;
  var ntop = pos.top;
  $('#p'+id).css({'top':ntop,'left':nleft}).show();
 }
 function validatechk1()
 {
	 var chks = document.getElementsByName('ref');
	 var checkCount = 0;
	 for (var i = 0; i < chks.length; i++)
	 {
	 if (chks[i].checked)
	 {
		 checkCount++;
	 }
	 }
	 if (checkCount == 0)
	 {
		 alert("Please select one transaction.");
		 return false;
	 }
	 else
	 {
		 
		 var selected = new Array();
		 $('#refund_only_box input:checked').each(function() {
		     selected.push($(this).val());
		 
		 });
		 var arr_cnt = selected.length;
		 var temp;
		 for(i=0;i<arr_cnt;i++)
		 {
			 if(i==0)
				 temp = selected[i];
			 else
				 temp+="_"+selected[i]; 
		 }
		 //alert(temp);
		 var x=document.getElementById("ref_form");
		 x.action="amazon_refund_referral_comm.php?ref_comm_id="+temp;
		 x.submit();
	 }
	 return true;
 }
 
</script>